package binary_search.leetcode_287_medium;

public class BestSolution {
    public int findDuplicate(int[] nums) {
        if(nums == null || nums.length == 0){
            return 0;
        }
        int fast = 0;
        int slow = 0;
        while(true){
            fast = nums[nums[fast]];    //fast = fast.next.next;
            slow = nums[slow];          //slow = slow.next;
            if(fast == slow){
                break;
            }
        }
        fast = 0;
        while(nums[fast] != nums[slow]){
            fast = nums[fast];
            slow = nums[slow];
        }
        return nums[slow];
    }
}
